import Vue from 'vue'
import VueRouter from 'vue-router'
import common from './common'

// import wxsdk from '../utils/wxsdk'
import store from '../store'

import TryNewProducts from "./TryNewProducts";
Vue.use(VueRouter)

const routes = [
  ...common,
  ...TryNewProducts,
]

// // 解决报错
// const originalPush = VueRouter.prototype.push
// const originalReplace = VueRouter.prototype.replace
// // push
// VueRouter.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }
// // replace
// VueRouter.prototype.replace = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
//   return originalReplace.call(this, location).catch(err => err)
// }

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // let token = localStorage.getItem('token')
  if (to.meta.title) {
    document.title = to.meta.title
    document.body.scrollTop = 0
    document.documentElement.scrollTop = 0


    if (to.query.from_uid && to.query.id) {
      sessionStorage.setItem('from_uid', to.query.from_uid)
      let share = {
        name: to.name,
        id: to.query.id
      }
      sessionStorage.setItem("share", JSON.stringify(share))
    }
    if (!store.state.token) {
      //sessionStorage.setItem('url', to.fullPath)
      if (to.path == '/MobileLogin') {
        next();
      } else {
        if (to.path == '/login') {//如果是登录页面路径，就直接next()
          next();
        } else {//不然就跳转到登录；
          next('/login');
        }
      }



    } else {
      next()
    }

    // next()
    // getWxUserInfo
  }
  // if (token) {
  //   to.name == 'login' ? next('./') : next()
  // } else {
  //   to.name != 'login' ? next({ name: 'login' }) : next()
  // }


})

export default router
